package org.robot.utils;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.robot.component.Attachment;

public class XmlUtil {
	/**
	 * 生成xml，paperAttachmentList里面的元素为paperAttachment，paperAttachment上级为issueAttachment，上2级为journalAttachment，
	 * 如果不是调用重载方法。<br>
	 * paperAttachment、issueAttachment、journalAttachment除了链接以外，infoMap都存有生成xml所需要的信息，paper的infoMap中有一个key为references
	 * 的值为String类型的List，其他的值均为String类型
	 * @author pdz
	 * @date 2019年1月23日
	 * @param paperAttachmentList
	 * @param xmlFile
	 */
	public static void generateXml(List<Attachment> paperAttachmentList, File xmlFile) {
		generateXml(paperAttachmentList, xmlFile, 1, 2);
	}
	
	/**
	 * 生成xml，paperAttachmentList里面的元素为paperAttachment<br>
	 * paperAttachment、issueAttachment、journalAttachment除了链接以外，infoMap都存有生成xml所需要的信息，paper的infoMap中有一个key为references
	 * 的值为String类型的List，其他的值均为String类型
	 * @author pdz
	 * @date 2019年1月23日
	 * @param paperAttachmentList
	 * @param xmlFile
	 * @param issuePrevNum 上几层可以得到issueAttachment
	 * @param journalPrevNum 上几层可以得到journalAttachment
	 */
	public static void generateXml(List<Attachment> paperAttachmentList, File xmlFile, int issuePrevNum, int journalPrevNum) {
		if (!xmlFile.getParentFile().exists())
			xmlFile.getParentFile().mkdirs();
		Document doc = DocumentHelper.createDocument();
		Element journalList = doc.addElement("journalList");
		paperAttachmentList.forEach(paperAttach -> saveToXml(paperAttach, issuePrevNum, journalPrevNum, journalList));
		try (FileWriter out = new FileWriter(xmlFile);) {
			doc.write(out);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@SuppressWarnings("unchecked")
	private static void saveToXml(Attachment paperAttach, int issuePrevNum, int journalPrevNum, Element journalList) {
		Attachment issueAttach = paperAttach.prevAttachment(issuePrevNum);
		Attachment journalAttach = paperAttach.prevAttachment(journalPrevNum);
		Element publication = (Element) journalList.selectSingleNode("//publication[publicationUrl='" + issueAttach.getUrl() + "']");
		if (publication == null) {
			Element journal = journalList.addElement("journal").addAttribute("method", "add");
			journal.addElement("journalUrl").addText(journalAttach.getUrl());
			journal.addElement("fullEnglishName").addText((String) journalAttach.get("fullEnglishName"));
			journal.addElement("fullChineseName").addText((String) journalAttach.get("fullChineseName"));
			journal.addElement("shortEnglishName").addText((String) journalAttach.get("shortEnglishName"));
			journal.addElement("issnPrint").addText((String) journalAttach.get("issnPrint"));
			journal.addElement("issnOnline").addText((String) journalAttach.get("issnOnline"));
			
			publication = journal.addElement("publication").addAttribute("method", "add");
			publication.addElement("publicationUrl").addText(issueAttach.getUrl());
			publication.addElement("issueName").addText((String) issueAttach.get("issueName"));
			publication.addElement("pubYear").addText((String) issueAttach.get("pubYear"));
		}
		Element paper = publication.addElement("paper").addAttribute("method", "addOrUpdate");
		paper.addElement("paperUrl").addText(paperAttach.getUrl());
		paper.addElement("title").addText((String) paperAttach.get("title"));
		paper.addElement("authorName").addText((String) paperAttach.get("authorName"));
		paper.addElement("keywords").addText((String) paperAttach.get("keywords"));
		paper.addElement("paperAbstract").addText((String) paperAttach.get("paperAbstract"));
		paper.addElement("fileName").addText((String) paperAttach.get("fileName"));
		paper.addElement("imageFileName").addText((String) paperAttach.get("imageFileName"));
		paper.addElement("fileUrl").addText((String) paperAttach.get("fileUrl"));
		paper.addElement("doi").addText((String) paperAttach.get("doi"));
		paper.addElement("affs").addText((String) paperAttach.get("affs"));
		paper.addElement("startPage").addText((String) paperAttach.get("startPage"));
		paper.addElement("endPage").addText((String) paperAttach.get("endPage"));
		List<String> referenceList = (List<String>) paperAttach.get("references");
		if (referenceList != null && !referenceList.isEmpty()) {
			Element references = paper.addElement("references");
			referenceList.forEach(ref -> references.addElement("reference").addText(ref));
		}
	}

}
